package es.ats.codington.web.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventServiceImpl;
import es.ats.codington.festival.services.PlaceServiceImpl;
import es.ats.codington.festival.services.VisitorServiceImpl;

/**
 * Servlet implementation class UpdateEventServlet.
 */
@WebServlet("/UpdateEventServlet")
public class UpdateEventServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The id. */
	static int id;

    /**
     * Instantiates a new update event servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public UpdateEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession(true);
	 //  	int IdEvent =(Integer) session.getAttribute("idEvent");
	   	
		int idEvent = Integer.parseInt(request.getParameter("idEvent"));
		id = idEvent;
		try{		
			EventServiceImpl eve = new EventServiceImpl();
			Event evento = new Event();
			
			
			evento = eve.searchEventbyID(idEvent);
			
	    	session.setAttribute("MyEvent", evento);

	    	session.setAttribute("Session", 4);
			RequestDispatcher rd=request.getRequestDispatcher("updateEvent.jsp");  
	        rd.include(request,response); 
	        
	        
			
		
	    }catch(Exception theException){
	        System.out.println(theException); 
	   }
			
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 // Get information
		EventServiceImpl eve = new EventServiceImpl();
    	Event event = new Event();
    	int row=0;

				 
		String name = request.getParameter("event");
		String description = request.getParameter("description");
		String duration = request.getParameter("duration");
		String idplace = request.getParameter("locaction");
		String eventype = request.getParameter("type");
		String capac = request.getParameter("capacity");
		         
		int placeId = Integer.parseInt(idplace);   
		int capacity = Integer.parseInt(capac);
		int dur = Integer.parseInt(duration);
		
		/*
         *  REGISTER EVENT
         */ 
        try{
        	event.setName(name);
        	event.setDescription(description);
        	event.setDuration(dur);
        	event.setTypeEvent(eventype);
        	event.setCapacity(capacity);
        	event.setIdPlace(placeId);
        	event.setId(id);
        
        	row= eve.updateEvent(event);
        
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        
        if(row!=0){
       //	 response.sendRedirect("listEventAdmin.jsp");
       	 //*RequestDispatcher rd=request.getRequestDispatcher("login.jsp");  
            //*rd.forward(request,response); 
       	 
       	 ListAllEventServlet list= new ListAllEventServlet();
       	 list.sendListAllEvent(request, response);

        }
        else{
       	 response.sendRedirect("registerEvent.jsp");
       	 //*RequestDispatcher rd=request.getRequestDispatcher("home.jsp");  
            //*rd.include(request,response); 
        }
	}

}
